package com.right.health.app.ui.main.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.magic.basic.utils.ToastUtil;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.base.BasePresenterFragment;
import com.right.health.app.bean.HomeItem;
import com.right.health.app.common.DataCommon;
import com.right.health.app.ui.common.KnowledgeActivity;
import com.right.health.app.ui.common.MoreServiceActivity;
import com.right.health.app.ui.common.SearchActivity;
import com.right.health.app.ui.main.MainActivity;
import com.right.health.app.ui.main.adapter.HomeFeature1Adapter;
import com.right.health.app.ui.main.adapter.HomeFeature2Adapter;
import com.right.health.app.ui.main.adapter.HomeFeature3Adapter;
import com.right.health.app.ui.me.FamilyActivity;
import com.right.health.app.ui.me.ServicePackActivity;
import com.right.health.app.ui.registered.ChooseDepartmentActivity;
import com.right.health.app.ui.registered.FindDoctorActivity;
import com.right.health.app.ui.registered.PayClinicActivity;
import com.right.health.app.widget.GridViewEx;
import com.right.health.app.widget.HomeBannerView;
import com.right.health.app.widget.banner.anim.select.ZoomInEnter;
import com.right.health.app.widget.banner.base.BaseBanner;

/*
 * Created by hopeliao on 2019-09-23
 *
 */
public class HomeFragment extends BasePresenterFragment implements
        View.OnClickListener {

    private HomeFeature1Adapter mFeature1Adapter;

    private HomeFeature2Adapter mFeature2Adapter;

    private HomeFeature3Adapter mFeature3Adapter;

    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle, String s) {
        return layoutInflater.inflate(R.layout.home, viewGroup, false);
    }

    @Override
    public void findView() {
        GridViewEx featuresGrid1 = (GridViewEx) findViewById(R.id.features1_grid);
        mFeature1Adapter = new HomeFeature1Adapter(getActivity());
        featuresGrid1.setAdapter(mFeature1Adapter);
        featuresGrid1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HomeItem homeItem = (HomeItem) parent.getItemAtPosition(position);
                if (homeItem != null) {
                    switch (homeItem.name) {
                        case "挂号":
                            startIntent(ChooseDepartmentActivity.class);
                            break;
                        case "更多服务":
                            startIntent(MoreServiceActivity.class);
                            break;
                        case "诊间支付":
                            startIntent(PayClinicActivity.class);
                            break;
                        case "病历查询":
                           MainActivity.makeActivity(getActivity(), MainActivity.TAB_MEDICAL);
                           break;
                        default:
                            ToastUtil.show(App.getInstance(), R.string.not_yet_opened);
                            break;
                    }
                }
            }
        });

        GridViewEx featuresGrid2 = (GridViewEx) findViewById(R.id.features2_grid);
        mFeature2Adapter = new HomeFeature2Adapter(getActivity());
        featuresGrid2.setAdapter(mFeature2Adapter);
        featuresGrid2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HomeItem homeItem = (HomeItem) parent.getItemAtPosition(position);
                if (homeItem != null) {
                    switch (homeItem.name) {
                        case "找医生":
                            startIntent(FindDoctorActivity.class);
                            break;
                        case "我的家人":
                            startIntent(FamilyActivity.class);
                            break;
                        case "线上复诊":
                            ChooseDepartmentActivity.applyActivity(getActivity(), "jiuzhen");
                            break;
                    }
                }
            }
        });

        GridViewEx featuresGrid3 = (GridViewEx) findViewById(R.id.features3_grid);
        mFeature3Adapter = new HomeFeature3Adapter(getActivity());
        featuresGrid3.setAdapter(mFeature3Adapter);
        featuresGrid3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (!isAdded()) {
                    return;
                }
                HomeItem homeItem = (HomeItem) parent.getItemAtPosition(position);
                if (homeItem != null) {
                    switch (homeItem.name) {
                        case "知识课堂":
                            startIntent(KnowledgeActivity.class);
                            break;
                        case "家庭医生":
                            startIntent(ServicePackActivity.class);
                            break;
                        case "便民服务":
                        case "健康商场":
                        case "智能导诊":
                        case "症状自诊":
                        case "用药安全":
                        case "药品查询":
                        case "社区医院":
                            ToastUtil.show(App.getInstance(), R.string.not_yet_opened);
                            break;
                    }
                }
            }
        });

        HomeBannerView mHomeBannerView = (HomeBannerView) findViewById(R.id.banner_view);
        mHomeBannerView.setAutoScrollEnable(true);
        mHomeBannerView.setSelectAnimClass(ZoomInEnter.class);
        mHomeBannerView.setOnItemClickListener(new BaseBanner.OnBannerItemClickListener() {
            @Override
            public void onBannerItemClick(int position) {

            }
        });

        mHomeBannerView.pauseScroll();
        mHomeBannerView.setDataSource(DataCommon.getBannerList());
        mHomeBannerView.startScroll();

        findViewById(R.id.ll_search).setOnClickListener(this);
        findViewById(R.id.btn_search).setOnClickListener(this);
    }

    @Override
    public void initialize() {
        mFeature1Adapter.setDataSource(DataCommon.getHomeFeature1());
        mFeature2Adapter.setDataSource(DataCommon.getHomeFeature2());
        mFeature3Adapter.setDataSource(DataCommon.getHomeFeature3());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ll_search || v.getId() == R.id.btn_search) {
            startIntent(SearchActivity.class);
        }
    }
}
